import axios from "axios";
import { cityData, countryData, holidayList } from "../mocks/mockData";
export const getCountryData = () => {
  return new Promise((resolve, reject) => {
    try {
      setTimeout(() => {
        resolve(countryData);
      });
    } catch (error) {
      reject(error);
    }
  });
};
export const getStayInfo = (params: any) => {
  const { region, lat, lng } = params;
  return axios.get(`/baidu/place/v2/search?query=酒店&region=${region}&filter=sort_name:distance&center=${lat},${lng}&output=json&ak=5n0XIskvDxyeGx6NcSyUGjPFgYXnATBz`).then((res: any) => {
    return res?.data?.results
  })
};
export const getCityData = () => {
  return new Promise((resolve, reject) => {
    try {
      setTimeout(() => {
        resolve(cityData);
      });
    } catch (error) {
      reject(error);
    }
  });
};
function shuffle(arr) {
  var length = arr.length,
    randomIndex,
    temp;
  while (length) {
    randomIndex = Math.floor(Math.random() * (length--));
    temp = arr[randomIndex];
    arr[randomIndex] = arr[length];
    arr[length] = temp
  }
  return arr;
}

export const getWeather = (cityName: string) => {
  return axios
    .get(
      `https://geoapi.qweather.com/v2/city/lookup?location=${cityName}&key=6e60bd34355449ca9dc6e464d98480bc`
    )
    .then(function (response) {
      const id = response?.data?.location[0]?.id;
      const location = response?.data?.location[0];
      return axios
        .get(
          `https://devapi.qweather.com/v7/weather/7d?location=${id}&key=6e60bd34355449ca9dc6e464d98480bc`
        )
        .then(function (response) {
          const newResults = shuffle(response?.data?.daily);
          const _data = {
            now: newResults[0],
            location
          }
          return _data;
        })
        .catch(function (error) {
          return error;
        });
    })
    .catch(function (error) {
      return error;
    });
};
export const getHolidayData = (countryName: string) => {
  return new Promise((resolve, reject) => {
    try {
      const _data = holidayList?.filter(item => item?.name === countryName);
      resolve(_data[0]?.value);
    } catch (error) {
      reject(error);
    }
  });
};

